/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Arduino;

import Clases.SignosVitales;
import Funciones.FuncionesSignosVitales;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Enumeration;
import javafx.scene.chart.XYChart;
import sistema.clinica.riobamba.desktop.ServicioOnlineController;

/**
 *
 * @author xavier
 */
public class UtilArduino implements SerialPortEventListener {

    BufferedReader input = null;
    SerialPort serialPort;
    private final String PORT_NAME = "COM5";
    private static final int TIME_OUT = 2000;
    private static final int DATA_RATE = 115200;
    private ServicioOnlineController soController;

    double hcgX;
    double hcgY;
    int pr;
    int sp;

    SignosVitales sv = new SignosVitales();

    public ServicioOnlineController getSoController() {
        return soController;
    }

    public void setSoController(ServicioOnlineController soController) {
        this.soController = soController;
    }

    public BufferedReader getInput() {
        return input;
    }

    public void setInput(BufferedReader input) {
        this.input = input;
    }

    public SerialPort getSerialPort() {
        return serialPort;
    }

    public void setSerialPort(SerialPort serialPort) {
        this.serialPort = serialPort;
    }

    public Double getHcgX() {
        return hcgX;
    }

    public void setHcgX(Double hcgX) {
        this.hcgX = hcgX;
    }

    public Double getHcgY() {
        return hcgY;
    }

    public void setHcgY(Double hcgY) {
        this.hcgY = hcgY;
    }

    public int getPr() {
        return pr;
    }

    public void setPr(int pr) {
        this.pr = pr;
    }

    public int getSp() {
        return sp;
    }

    public void setSp(int sp) {
        this.sp = sp;
    }

    public SignosVitales getSv() {
        return sv;
    }

    public void setSv(SignosVitales sv) {
        this.sv = sv;
    }

    public void ArduinoConnection() {

        CommPortIdentifier portId = null;
        Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
        while (portId == null && portEnum.hasMoreElements()) {
            CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
            if (PORT_NAME.equals(currPortId.getName())) {
                portId = currPortId;
                break;
            }
        }
        if (portId == null) {
            return;
        }
        try {
            serialPort = (SerialPort) portId.open(this.getClass().getName(), TIME_OUT);
            serialPort.setSerialPortParams(DATA_RATE,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);
            serialPort.addEventListener(this);
            serialPort.notifyOnDataAvailable(true);
        } catch (Exception e) {
        }
    }

    @Override
    public void serialEvent(SerialPortEvent spe) {
//        try {
//            switch (spe.getEventType()) {
//                case SerialPortEvent.DATA_AVAILABLE:
//                    if (input == null) {
//
//                        input = new BufferedReader(
//                                new InputStreamReader(
//                                        serialPort.getInputStream()));
//                    }
//                    String inputLine = input.readLine();
//                    String[] datos = inputLine.split(";");
//                    //System.err.println(datos[0]+" "+datos[1]);
//                    if (datos.length == 2) {
//                        hcgX = Double.parseDouble(datos[0]);
//                        hcgY = Double.parseDouble(datos[1]);
//                    //pr = Integer.parseInt(datos[2]);
//                        //sp = Integer.parseInt(datos[3]);
//                        //System.out.println(inputLine);
////                    sv.setHcg(hcg);
////                    sv.setPr(pr);
////                    sv.setPs(pr);
//
//                        //FuncionesSignosVitales.insertar(sv);
////aqui aumentar puntos en el grafico                        
//                        soController.setX(hcgX);
//                        soController.setY(hcgY);
//                        soController.getSeries().getData().add(new XYChart.Data<Number, Number>(hcgX, hcgY));
//                        if (hcgX > 5040) {
//                            soController.getSeries().getData().remove(0);
//                            //hourDataSeries.getData().remove(0);
//                        }
//                        //if (timeInHours > 5000 ) {
//                        if (hcgX > 5000) {                            
//                            soController.getxAxis().setLowerBound(soController.getxAxis().getLowerBound() + 40);
//                            soController.getxAxis().setUpperBound(soController.getxAxis().getUpperBound() + 40);
//                        }
//                        soController.setOxi(sp);
//                        soController.setPul(pr);
//                    }
//
//                    break;
//                default:
//                    break;
//            }
//        } catch (Exception e) {
//            System.err.println(e.toString() + "    " + e.getStackTrace());
//        }
    }

}
